<template>
  <div>
    <div class="block">
      <el-tree
        ref="tree"
        :data="menuTree"
        node-key="id"
        :show-checkbox="true"
        :default-expand-all="false"
        :highlight-current="true"
        :expand-on-click-node="false"
        :default-expanded-keys="defaultExpandedKeys"
        :default-checked-keys="defaultCheckedKeys"
        @node-click="getCurrentKey()"
      >
        <span slot-scope="{ node, data}" class="custom-tree-node">
          <div>
            <span>{{ node.label }}</span>
            <i v-show="'{{node.sysAclStatus}}' === '0' || '{{node.sysAcModulelStatus}}' === '0'" class="el-icon-circle-check" />
          </div>
        </span>
      </el-tree>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="info" @click="dialogVisibleAuthorOff">取 消</el-button>
      <el-button type="danger" @click="authVipRoleDialogFormVisible">确定授权</el-button>
    </div>
  </div>
</template>

<script>
import Tip from '@/utils/tip'
import Role from '@/api/axios/api/author/role'
export default {
  name: 'AuthRolePermission',
  props: {
    hideAuthorCompBoolean: Boolean,
    vipUidObjAuthor: {},
    vipAuthComp: {},
    authRolePermissionObj: {}
  },
  data () {
    return {
      VipAuthorTitle: '',
      dialogVisibleAuthor: false,
      menuTree: [],
      // 默认展开
      defaultExpandedKeys: [],
      // 默认选中
      defaultCheckedKeys: [],
      // 所有权限 k 前缀+id, v 对象
      allAclMap: new Map()
    }
  },
  watch: {
    authRolePermissionObj: {
      deep: true,
      immediate: true,
      handler () {
        this.cleanAllData()
        this.recursivePrepareTreeData(this.authRolePermissionObj.authMenuTreeData)
      }
    }
  },
  methods: {
    cleanAllData () {
      this.menuTree = []
      this.VipAuthorTitle = ''
      // 默认展开
      this.defaultExpandedKeys = []
      // 默认选中
      this.defaultCheckedKeys = []
      // 所有权限 k 前缀+id, v 对象
      this.allAclMap = new Map()
    },
    VipAuthorClose () {
      this.cleanAllData()
    },
    handleClose (done) {
      this.cleanAllData()
    },
    dialogVisibleAuthorOff () {
      this.$emit('closeInnerVisibleRolePermission', this.roleEditRuleForm)
    },
    authVipRoleDialogFormVisible () {
      const vip = {}
      const acls = []
      const len = 'aclPrefix'.length
      const checkedNodes = this.$refs.tree.getCheckedNodes()
      if (checkedNodes && checkedNodes.length > 0) {
        checkedNodes.forEach(value => {
          if (value.id.indexOf('aclPrefix') !== -1) {
            const acl = {
              sysAclId: value.id.substring(len),
              sysAclPermissionType: value.sysAclPermissionType
            }
            acls.push(acl)
          }
        })
      }
      vip.acls = acls
      vip.sysRoleUuid = this.authRolePermissionObj.sysRoleUuid
      Role.aclsToRole(vip).then(res => {
        Tip.success(res.data)
        setTimeout(() => {
          this.$emit('closeInnerVisibleRolePermission', this.roleEditRuleForm)
        }, 1000)
      })
    },
    recursivePrepareTreeData (param) {
      // 建立个树形结构,需要定义个最顶层的父节点renderContent
      const parent = []
      // this.$refs.tree
      if (param && param.length > 0) {
        param.forEach(param => {
          const fathChild = this.moduleList(param.aclModuleList)
          if (param.aclList && param.aclList.length > 0) {
            param.aclList.forEach(param => {
              const obj = {
                label: param.sysAclName,
                sysAclModuleId: param.sysAclModuleId,
                sysAclAction: param.sysAclAction,
                sysAclRouter: param.sysAclRouter,
                sysAclUrl: param.sysAclUrl,
                sysAclType: param.sysAclType,
                sysAclPermissionType: param.sysAclPermissionType,
                sysAclSeq: param.sysAclSeq,
                sysAclUuid: param.sysAclUuid,
                sysAclStatus: param.sysAclStatus,
                id: 'aclPrefix' + param.id
              }
              if (param.checked) {
                this.defaultCheckedKeys.push('aclPrefix' + param.id)
              }
              if (!param.hasAcl) {
                obj.disabled = false
              }
              this.allAclMap.set('aclPrefix' + param.id, obj)
              fathChild.push(obj)
            })
          }
          const obj = {
            disabled: false,
            label: param.sysAclModuleName,
            sysAcModulelStatus: param.sysAcModulelStatus,
            sysAclModuleUuid: param.sysAclModuleUuid,
            sysAclModuleParentId: param.sysAclModuleParentId,
            sysAclModuleSeq: param.sysAclModuleSeq,
            sysAclPermissionType: param.sysAclPermissionType,
            sysAclModuleLevel: param.sysAclModuleLevel,
            id: 'modulePrefix' + param.id,
            children: fathChild
          }
          if (param.aclModuleList && param.aclModuleList.length > 0) {
            this.defaultExpandedKeys.push('modulePrefix' + param.id)
          }
          this.allAclMap.set('modulePrefix' + param.id, obj)
          parent.push(obj)
        })
      }
      this.menuTree = parent
    },
    moduleList (moduleList, aclList) {
      const child = []
      if (aclList && aclList.length > 0) {
        aclList.forEach(acl => {
          const obj = {
            label: acl.sysAclName,
            sysAclModuleId: acl.sysAclModuleId,
            sysAclAction: acl.sysAclAction,
            sysAclRouter: acl.sysAclRouter,
            sysAclUrl: acl.sysAclUrl,
            sysAclType: acl.sysAclType,
            sysAclPermissionType: acl.sysAclPermissionType,
            sysAclSeq: acl.sysAclSeq,
            sysAclUuid: acl.sysAclUuid,
            sysAclStatus: acl.sysAclStatus,
            id: 'aclPrefix' + acl.id
          }
          if (acl.checked) {
            this.defaultCheckedKeys.push('aclPrefix' + acl.id)
            this.defaultExpandedKeys.push('aclPrefix' + acl.id)
          }
          if (!acl.hasAcl) {
            obj.disabled = true
          }
          this.allAclMap.set('aclPrefix' + acl.id, obj)
          child.push(obj)
        })
      }
      if (moduleList && moduleList.length > 0) {
        moduleList.forEach(param => {
          const obj = {
            disabled: false,
            label: param.sysAclModuleName,
            sysAcModulelStatus: param.sysAcModulelStatus,
            sysAclModuleUuid: param.sysAclModuleUuid,
            sysAclModuleParentId: param.sysAclModuleParentId,
            sysAclModuleSeq: param.sysAclModuleSeq,
            sysAclPermissionType: param.sysAclPermissionType,
            sysAclModuleLevel: param.sysAclModuleLevel,
            id: 'modulePrefix' + param.id,
            children: this.moduleList(param.aclModuleList, param.aclList)
          }
          if (param.aclModuleList && param.aclModuleList.length > 0) {
            this.defaultExpandedKeys.push('modulePrefix' + param.id)
          }
          this.allAclMap.set('modulePrefix' + param.id, obj)
          child.push(obj)
        })
      }
      return child
    }
  }
}
</script>

<style scoped>

</style>
